//
//  NSString+JSHandler.m
//  ShopMobile
//
//  Created by mac on 17/10/20.
//  Copyright © 2017年 soubao. All rights reserved.
//

#import "NSString+JSHandler.h"

@implementation NSString (JSHandler)
-(BOOL(^)(void))JS_RegularPhoneNumber{
    return ^(){ // ^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$
        NSString *MOBILE = @"^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
        return [regextestmobile evaluateWithObject:self];
    };
    
}
-(BOOL(^)(void))JS_RegularNumber{
    return ^(){ // ^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$
        NSString *MOBILE = @"[0-9]*";
        NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
        return [regextestmobile evaluateWithObject:self];
    };
    
}
-(BOOL(^)(void))JS_RegularEmail{
    return ^(){ // ^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$
        NSString *MOBILE = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
        return [regextestmobile evaluateWithObject:self];
    };
    
}

-(BOOL(^)(void))JS_RegularUrl{
    return ^(){ // ^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$
        NSString *MOBILE = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
        NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
        return [regextestmobile evaluateWithObject:self];
    };
    
}



-(BOOL(^)(void))JS_RegularIdentityCard{
    return ^(){ // ^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$
        NSString *MOBILE = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
        NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
        return [regextestmobile evaluateWithObject:self];
    };
    
}
-(BOOL(^)(void))JS_RegularBankCard{
    return ^(){ // ^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$
       
        if(self.length==0){
            return NO;
        }
        NSString *digitsOnly = @"";
        char c;
        for (int i = 0; i < self.length; i++){
            c = [self characterAtIndex:i];
            if (isdigit(c))
            {
                digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
            }
        }
        int sum = 0;
        int digit = 0;
        int addend = 0;
        BOOL timesTwo = false;
        for (NSInteger i = digitsOnly.length - 1; i >= 0; i--){
            digit = [digitsOnly characterAtIndex:i] - '0';
            if (timesTwo){
                addend = digit * 2;
                if (addend > 9) {
                    addend -= 9;
                }
            }else {
                addend = digit;
            }
            sum += addend;
            timesTwo = !timesTwo;
        }
        NSInteger modulus = sum % 10;
        if (modulus == 0) return YES;
        return NO;
    };
    
}

-(NSString *(^)(void))JS_FormatterBankCardNum{
    return ^NSString *(){
        
        NSString *tempStr = self;
        NSInteger size =(tempStr.length / 4);
        NSMutableArray *tmpStrArr = [[NSMutableArray alloc] init];
        if (tempStr.length % 4 == 0) {
            size -= 1;
        }
        for (int n = 0;n < size; n++){
            
            [tmpStrArr addObject:@"****"];
            
        }
        if (tempStr.length % 4 == 0) {
            [tmpStrArr addObject:[tempStr substringWithRange:NSMakeRange(size*4, 4)]];
        }else{
            [tmpStrArr addObject:[tempStr substringWithRange:NSMakeRange(size*4, (tempStr.length % 4))]];
        }
        
        tempStr = [tmpStrArr componentsJoinedByString:@" "];
        return tempStr;
    };
    
}

-(BOOL(^)(void))JS_RegularPrice{
    return ^(){
        if (self.length) {
            NSString *stringRegex = @"(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,8}(([.]\\d{0,2})?)))?";//一般格式 d{0,8} 控制位数
            
            NSPredicate *money = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", stringRegex];
            
            return [money evaluateWithObject:self];
        }
        return NO;
    };
}
-(NSString *(^)(void))JS_DecodedString{
    return ^NSString *(){
        BeginIgnoreDeprecatedWarning
          NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)self, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
        EndIgnoreDeprecatedWarning
        return decodedString;
    };
}
- (NSString *(^)(void))JS_Url_Dencode{
    return ^NSString *(){
        BeginIgnoreDeprecatedWarning
        NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)self,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",NULL,kCFStringEncodingUTF8));
        EndIgnoreDeprecatedWarning
        return encodedString;
    };
}
/*!
 *  中文编码处理
 */
- (NSString *)unicodeString{
    
    NSString *tempStr1 = [self stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    
    
    NSPropertyListFormat format = NSPropertyListOpenStepFormat;
    
    NSString *returnStr = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:&format error:nil];
    
    
    
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
    
    
}




-(NSString * (^)(NSString *format))time_dateWithFormat{
    return ^(NSString *format){
        
        NSDate *date = [self js_time_dateWithFormatString:format];
        NSInteger timeNunber = [date timeIntervalSince1970];
        return [NSString stringWithFormat:@"%zd",timeNunber];
    };
}

-(NSString * (^)(NSString *format))time_FormatWithDate{
    return ^(NSString *format){
        return [NSString ba_time_getDateWithTimeStamp:self formatString:format];
    };
}
- (NSDate *)js_time_dateWithFormatString:(NSString *)formatString
{
    if (self == nil || [self isEqualToString:@""] || formatString == nil || [formatString isEqualToString:@""])
    {
        return nil;
    }
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = formatString;
    NSDate *date = [fmt dateFromString:self];
    return date;
}
+ (NSString *)ba_time_getDateWithTimeStamp:(NSString *)timeStamp
                              formatString:(NSString *)formatString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:formatString];
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    // 时间戳转换
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timeStamp intValue]];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
}


@end
